package com.aooci.ordercat.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.aooci.ordercat.dao.OrderedItemDAO;
import com.aooci.ordercat.domain.OrderedItem;
import com.aooci.ordercat.vo.OrderedItemVo;

/**
 * A data access object (DAO) providing persistence and search support for
 * Account entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.bitongline.lgs.dao.Account
 */
public class OrderedItemDAOImpl extends AbstractBaseDAOImpl<OrderedItem, Integer> implements OrderedItemDAO {

	public OrderedItemDAOImpl() {
		super(OrderedItem.class);
	}

	private static final long serialVersionUID = -4236573107157049581L;
	private static final Logger log = Logger.getLogger(OrderedItemDAOImpl.class);
	
	
	@Override
	public List<OrderedItemVo> selectByOrdercatId(final int ordercatId) {
	    return getHibernateTemplate().execute(new HibernateCallback<List<OrderedItemVo>>(){
	        @SuppressWarnings("unchecked")
			@Override  
	        public List<OrderedItemVo> doInHibernate(Session session)  
	            throws SQLException {  
	        	Query query = session.getNamedQuery("ORDERED_ITEM.selectByOrdercatId");
	        	query.setParameter("ordercatId", ordercatId);
	            return (List<OrderedItemVo>)query.list();  
	        }  
	    });
	}


	@Override
	public OrderedItem findOrderedItem(final int ordercatId, final int itemId) {
	    return getHibernateTemplate().execute(new HibernateCallback<OrderedItem>(){
	        @Override  
	        public OrderedItem doInHibernate(Session session)  
	            throws SQLException {  
	        	Query query = session.getNamedQuery("ORDERED_ITEM.findOrderedItem");
	        	query.setParameter("ordercatId", ordercatId);
	        	query.setParameter("itemId", itemId);
	        	@SuppressWarnings("unchecked")
				List<OrderedItem> list = query.list();
	            return list.isEmpty()? null : list.get(0);  
	        }  
	    });
	}


	@Override
	public float getOrderTotal(final int ordercatId) {
	    return getHibernateTemplate().execute(new HibernateCallback<Float>(){
	        @Override  
	        public Float doInHibernate(Session session)  
	            throws SQLException {  
	        	Query query = session.getNamedQuery("ORDERED_ITEM.getTotalOfOrderItems");
	        	query.setParameter("ordercatId", ordercatId);
	        	Object result = query.uniqueResult();
	            if(result == null){
	            	return (float) 0.0;
	            }
	        	return Float.valueOf(result.toString());  
	        }  
	    });
	}


	@Override
	public List<OrderedItemVo> selectByOrdercatIdAndSortGroup(final int ordercatId, final int sortGroup) {
	    return getHibernateTemplate().execute(new HibernateCallback<List<OrderedItemVo>>(){
	        @SuppressWarnings("unchecked")
			@Override  
	        public List<OrderedItemVo> doInHibernate(Session session)  
	            throws SQLException {  
	        	Query query = session.getNamedQuery("ORDERED_ITEM.selectByOrdercatIdAndSortGroup");
	        	query.setParameter("ordercatId", ordercatId);
	        	query.setParameter("sortGroup", sortGroup);
	            return (List<OrderedItemVo>)query.list();  
	        }  
	    });
	}


	@Override
	public List<Integer> selectSortGroupByOrdercatId(final int ordercatId) {
	    return getHibernateTemplate().execute(new HibernateCallback<List<Integer>>(){
	        @SuppressWarnings("unchecked")
			@Override  
	        public List<Integer> doInHibernate(Session session)  
	            throws SQLException {  
	        	Query query = session.getNamedQuery("ORDERED_ITEM.selectSortGroupByOrdercatId");
	        	query.setParameter("ordercatId", ordercatId);
	            return (List<Integer>)query.list();  
	        }  
	    });
	}


	@Override
	public int resetModifiedCountByOrdercatId(final int ordercatId) {
	    return getHibernateTemplate().execute(new HibernateCallback<Integer>(){
	        @Override  
	        public Integer doInHibernate(Session session)  
	            throws SQLException {  
	        	Query query = session.getNamedQuery("ORDERED_ITEM.resetModifiedCountByOrdercatId");
	        	query.setParameter("ordercatId", ordercatId);
	            return query.executeUpdate();  
	        }  
	    });
	}

	@Override
	public void deleteByOrdercatId(final int ordercatId) {
		getHibernateTemplate().execute(new HibernateCallback<Object>() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.getNamedQuery("ORDERED_ITEM.deleteByOrdercatId");
	        	query.setParameter("ordercatId", ordercatId);
	        	query.executeUpdate();
				return null;
			}
		});
	}


	@Override
	public boolean hasKitchenItems(final int ordercatId) {
	    return getHibernateTemplate().execute(new HibernateCallback<Boolean>(){
	        @Override  
	        public Boolean doInHibernate(Session session)  
	            throws SQLException {  
	        	Query query = session.getNamedQuery("ORDERED_ITEM.hasKitchenItems");
	        	query.setParameter("ordercatId", ordercatId);
	        	return query.list().size() > 0 ? true : false;  
	        }  
	    });
	}


	@Override
	public boolean hasAddingKitchenItems(final int ordercatId) {
	    return getHibernateTemplate().execute(new HibernateCallback<Boolean>(){
	        @Override  
	        public Boolean doInHibernate(Session session)  
	            throws SQLException {  
	        	Query query = session.getNamedQuery("ORDERED_ITEM.hasAddingKitchenItems");
	        	query.setParameter("ordercatId", ordercatId);
	        	return query.list().size() > 0 ? true : false;  
	        }  
	    });
	}
}